package 常用的10种算法.kruskal;

import java.util.Arrays;

public class KruskalCase {
    private int edgeNum;//记录边的个数
    private char[] vertexs;//顶点数组
    private int[][] matrix;//邻接矩阵
    private static final int INF = Integer.MAX_VALUE;

    public static void main(String[] args) {
        char[] vertexs = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};
        //克鲁斯卡尔算法的邻接矩阵
        int matrix[][] = {
                /*A*//*B*//*C*//*D*//*E*//*F*//*G*/
                /*A*/ {0, 12, INF, INF, INF, 16, 14},
                /*B*/ {12, 0, 10, INF, INF, 7, INF},
                /*C*/ {INF, 10, 0, 3, 5, 6, INF},
                /*D*/ {INF, INF, 3, 0, 4, INF, INF},
                /*E*/ {INF, INF, 5, 4, 0, 2, 8},
                /*F*/ {16, 7, 6, INF, 2, 0, 9},
                /*G*/ {14, INF, INF, INF, 8, 9, 0}};
        KruskalCase kruskalCase = new KruskalCase(vertexs, matrix);
        kruskalCase.print();
        EData[] edges = kruskalCase.getEdges();
        System.out.println(Arrays.toString(edges));//未排序
        kruskalCase.sortEdge(edges);
        System.out.println("排序的结果");
        System.out.println(Arrays.toString(edges));
        kruskalCase.kruskal();
    }


    public KruskalCase(char[] vertexs, int[][] matrix) {
        int vlen = vertexs.length;
        //初始化顶点
        this.vertexs = new char[vlen];
        for (int i = 0; i < vertexs.length; i++) {
            this.vertexs[i] = vertexs[i];
        }
        //初始化边
        this.matrix = new int[vlen][vlen];
        for (int i = 0; i < vlen; i++) {
            for (int j = 0; j < vlen; j++) {
                this.matrix[i][j] = matrix[i][j];
            }
        }
        //统计边
        for (int i = 0; i < vlen; i++) {
            for (int j = i + 1; j < vlen; j++) {
                if (this.matrix[i][j] != INF) {
                    edgeNum++;
                }
            }
        }

    }

    public void kruskal() {
        int index = 0;
        int[] ends = new int[edgeNum];//用于保存已有"已有最小生成树"中的每个顶点在最小生成树中的终点
        //创建结果数组,保存最后的最小生成树
        EData[] rets = new EData[edgeNum];
        EData[] edges = getEdges();
        //排序
        sortEdge(edges);
        //遍历edges,要判断是否回路
        for (int i = 0; i < edgeNum; i++) {
            //获取这条边的两个顶点
            int p1 = getPosition(edges[i].start);
            int p2 = getPosition(edges[i].end);

            //获取p1,p2这两个顶点的终点
            int m = getEnd(ends, p1);
            int n = getEnd(ends, p2);
            if (m != n) {//不构成回路
                ends[m] = n;
                rets[index++] = edges[i];
            }

        }
        //统计打印最小生成树

        System.out.println("最小生成树:");
        for (int i = 0; i < index; i++) {
            System.out.println("最小生成树:" + rets[i]);
        }

    }

    //打印邻接矩
    public void print() {
        System.out.println("邻接矩阵:");
        for (int i = 0; i < vertexs.length; i++) {
            for (int j = 0; j < vertexs.length; j++) {
                System.out.printf("%12d\t", matrix[i][j]);
            }
            System.out.println();
        }
    }

    //对边进行排序

    /**
     * 冒泡排序
     *
     * @param edges
     */
    private void sortEdge(EData[] edges) {
        for (int i = 0; i < edges.length - 1; i++) {
            for (int j = 0; j < edges.length - 1; j++) {
                if (edges[j].weight > edges[j + 1].weight) {
                    EData temp = edges[j];
                    edges[j] = edges[j + 1];
                    edges[j + 1] = temp;
                }
            }
        }
    }

    /**
     * 传入 顶点的值,比如'A''B',
     *
     * @param ch
     * @return 返回ch对应的顶点下表
     */
    private int getPosition(char ch) {
        for (int i = 0; i < vertexs.length; i++) {
            if (vertexs[i] == ch) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 获取图中的边,放到EData[]中后面会遍历用到
     * 通过matrix邻接矩阵获得的
     */
    private EData[] getEdges() {
        int index = 0;
        EData[] edges = new EData[edgeNum];
        for (int i = 0; i < vertexs.length; i++) {
            for (int j = i + 1; j < vertexs.length; j++) {
                if (matrix[i][j] != INF) {
                    edges[index++] = new EData(vertexs[i], vertexs[j], matrix[i][j]);

                }
            }

        }
        return edges;
    }

    /**
     * 获取小标为i的顶点的终点,用于后面判断两个顶点的中点是否相同
     *
     * @param ends 记录了各个顶点对应的终点是哪个,ends 数组是在立案过程中逐步形成的
     * @param i    表示传入顶点对应的下标
     * @return 返回找个顶点他终点对应的下标
     */
    private int getEnd(int[] ends, int i) {
        while (ends[i] != 0) {
            i = ends[i];
        }
        return i;
    }

}

class EData {
    char start;//边的一个顶点
    char end;//另一个点
    int weight;//边的权值

    public EData(char start, char end, int weight) {
        this.start = start;
        this.end = end;
        this.weight = weight;
    }

    @Override
    public String toString() {
        return "EData{" +
                "start=" + start +
                ", end=" + end +
                ", weight=" + weight +
                '}';
    }
}
